import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

import {FtMenu} from '../../ft-comp/ft-menu';

import {Backend} from '../../providers/backend';
import {ImagePipe} from '../../pipes/pipes';
import {Files} from './tab-resources-files';
import {Details} from './tab-resources-details';
import {Group} from './tab-resources-group';

@Component({
  templateUrl: 'build/pages/tab-resources/tab-resources.html',
  directives: [FtMenu],
  pipes: [ImagePipe],
})
export class TabResources {

  static get parameters() {
    return [[NavController], [NavParams], [Backend]];
  }

  constructor(navCtrl, params, backend) {
    this.navCtrl = navCtrl;
    this.params = params;
    this.backend = backend;
    this.group=0;
    this.fuwu = 'http://gti.ftbj.net';
    this.did;
    this.result={};
    this.pageIndex;
    this.result1=[];
    this.menus = [
      { 'id': 3, 'name': 'White Paper' },
      { 'id': 4, 'name': 'Industry Briefing' },
      { 'id': 5, 'name': 'Meeting File' }
    ];

  }

  menuChanged(item) {
    console.log(item.id);
    if(item.id == 3){
      this.did = 3;
      this.group=0;
      this.pageIndex = 1;
      this.select_menu = 'Whitepaper';
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   this.result=result;
      //   this.resourcesWhitePaper = result.result[0];
      //   console.log(result.pageIndex);
      //   this.pageIndex=result.pageIndex;
      // });
              this.backend.dsQuery({
      entity: 'gtig$WhitePaper',
      viewName: '_local',
      query: `select e from gtig$WhitePaper e order by e.newstime desc`,
      first: 0,
      max: 20
    }).then((result) => {
      console.log(result);
      this.resourcesWhitePaper3 = result;
// this.result1=result;
// for(let i =0;i<this.result1.length;i++){
//   this.result1[i]['tit']=this.result1[i].title.replace( '+GTI+W',' GTI W' )
  
// }
      
    });
    }else if(item.id == 4){
      this.did = 4;
      this.group=2;
      this.select_menu = 'Industrybriefing';
      this.pageIndex = 1;
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      .then(result => {
        this.resourcesWhitePaper = result.result[0];
        console.log(this.resourcesWhitePaper);
        this.pageIndex=result.pageIndex;
      });
    } else{
      this.did = 5;
      this.group=1;
      this.select_menu = 'Meeting';
      this.pageIndex = 1;
          this.backend.dsQuery({
      entity: 'gtig$MeetingFile',
      viewName: '_local',
      query: `select e from gtig$MeetingFile e order by e.sort desc`,
      first: 0,
      max: 20
    }).then((result) => {
      console.log(result);
this.result1=result;
for(let i =0;i<this.result1.length;i++){
  this.result1[i]['tit']=this.result1[i].title.replace( '+GTI+W',' GTI W' )
  
}
      
    });
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   this.result=result;
      //   this.resourcesWhitePaper = result.result[0];
      //   console.log(result.pageIndex);
      //   this.pageIndex=result.pageIndex;
      // });
    }
  }

  toFile(id,classid) {
    if (this.select_menu =='Meeting') {
      this.navCtrl.push(Files, { id: id});
    } else {
      this.navCtrl.push(Details, { id: id,classid:classid });
    }
  }
   toFiles(item) {
   
      this.navCtrl.push(Details, { item: item });
    
  }
  fenzu(thid){
    this.navCtrl.push(Group, { 'thid': thid});
  }

  /**
   * 下拉刷新
   */
  doRefresh(refresher) {
      
    if(this.did == 3){
      this.did = 3;
      this.pageIndex = 1;
      // this.select_menu = 'Whitepaper';
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   this.resourcesWhitePaper = result.result[0];
      //   console.log(result.pageIndex);
      //   this.pageIndex=result.pageIndex;
      // });
    }else if(this.did == 4){
      this.did = 4;
      this.select_menu = 'Industrybriefing';
      this.pageIndex = 1;
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   this.resourcesWhitePaper = result.result[0];
      //   console.log(result.pageIndex);
      //   this.pageIndex=result.pageIndex;
      // });
    } else{
      // this.did = 5;
      // this.select_menu = 'Meeting';
      // this.pageIndex = 1;
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   this.result=result;
      //   this.resourcesWhitePaper = result.result[0];
      //   console.log(result.pageIndex);
      //   this.pageIndex=result.pageIndex;
      // });
    }
    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 500);
  }

  /**
   * 滚动加载
   */
  doInfinite(infiniteScroll) {
    this.pageIndex += 1;
    if(this.did == 3){
      this.select_menu = 'Whitepaper';
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   result.result[0].forEach((subject) => {
      //     this.resourcesWhitePaper.push(subject);
      //   });
        
      //   if(this.pageIndex >= result.pageTotal){
      //     this.pageIndex -=1;
      //   }
      // });
    }else if(this.did == 4){
      this.select_menu = 'Industrybriefing';
      // this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      // .then(result => {
      //   result.result[0].forEach((subject) => {
      //     this.resourcesWhitePaper.push(subject);
      //   });
        
      //   if(this.pageIndex >= result.pageTotal){
      //     this.pageIndex -=1;
      //   }
      // });
    }else{
      this.select_menu = 'Meeting';
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      .then(result => {
        result.result[0].forEach((subject) => {
          this.resourcesWhitePaper.push(subject);
        });
        if(this.pageIndex >= result.pageTotal){
          this.pageIndex -=1;
        }
      });
    }
    setTimeout(() => {
      console.log('Async operation has ended');
      infiniteScroll.complete();
    }, 500);
  }
}
